package components;

import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JComponent;

public class ComboBoxVerifier extends CustomVerifier {

    public ComboBoxVerifier(String message, JComponent component) {
        super(message, component);
    }

    @Override
    public boolean verify(JComponent input) {
        boolean ret = true;
        if (input instanceof JComboBox) {
            JComboBox cb = (JComboBox) input;
            ret = cb.getSelectedIndex() != 0;
        }
        return ret;
    }

    @Override
    public boolean shouldYieldFocus(JComponent input) {
        boolean valueOK = verify(input);
        
        if (input instanceof JComboBox) {
            if (!valueOK) {
                input.setBorder(BorderFactory.createLineBorder(Color.red));
            } else {
                input.setBorder(null);
                input.updateUI();
            }
        }
        
        return valueOK;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        shouldYieldFocus((JComponent) e.getSource()); //ignore return value
    }
    
}
